/**
* @Title: queryInstitution_list_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @author EX-xieqi001
* @date 2014年11月10日 下午2:38:47
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('queryInstitutionListQueryCtrl', ['$scope','$stateParams','$modalInstance','CommunityService','doctorInfoService','CheckRows','Modal',
	                                function($scope,$stateParams,$modalInstance,CommunityService,doctorInfoService,CheckRows,Modal){

        $scope.communityService = CommunityService;
        CommunityService.initCodeListDatas();
        
        var checkRows = [];
        if(CheckRows.checkInstitutionRows){
        	  for(var i=0;i< CheckRows.checkInstitutionRows.length;i++) {
              	checkRows.push(CheckRows.checkInstitutionRows[i]);
              }
        }
      
       
        $scope.institutionPage = {
        	total: 0,
        	currentPage: 0,
        	limit: 20
        };
        $scope.form = {};
        
		// 表头以及json对应的属性名称
		$scope.columns = [{
			label : '机构编号',
			map : 'providerCode',
			width : '25%'
		}, 
		{
			label : '机构名称',
			map : 'providerName',
			width : '25%'
		}, {
			label : '机构类别',
			map : 'organizationType',
			width : '25%'
		}, {
			label : '机构等级',
			map : 'hospitalLevel',
			width : '25%'
		}];
        //查询
        $scope.onSubmit = function(){
        	$scope.institutionPage.currentPage = 1;
        	query();
        }
        
        function query() {
        	var params = {
        			providerCode: $scope.form.medicalOrgCode,
        			providerName: $scope.form.medicalOrgName,
            		orgType: $scope.form.medicalOrgType,
            		orgLevel: $scope.form.medicalOrgLevel,
            		page:  $scope.institutionPage.currentPage,
            		pageSize:  $scope.institutionPage.limit
            };
            
            doctorInfoService.queryInstitution(params).success(function(res){
            	if(res){
            		$scope.tableData = res.result;
            		angular.forEach($scope.tableData,function(data,index){
            			angular.forEach(checkRows,function(checkData,index){
                			if(checkData.providerId === data.providerId ){
                				data.checked = true;
                			}
                		});
            		});
            		$scope.institutionPage.total = res.totals.total;
            	}
            });
        }
       
        //重置
        $scope.onReset = function() {
            $scope.form = {};
        };

        function findCheck(d) {
            for(var i=0;i< checkRows.length;i++) {
                var checkedData =  checkRows [i];
                if(checkedData.providerId == d.providerId) {
                    return i;
                }
            }
            return -1;
        }

        //checkbox 选中监听事件
        $scope.$on('fwatable.checked', function(e,d){
            if(findCheck(d) == -1) {
                checkRows.push(d);
            }
        });

        //checkbox 取消选中监听事件
        $scope.$on('fwatable.unchecked', function(e,d){
            var index = findCheck(d);
            if(index != -1) {
                checkRows.splice(index,1);
            }
        });
        
        //分页监听
		$scope.$on('pageTool.skipPage',function(e,v){
			$scope.institutionPage.currentPage = v;
			query();
		});
		
        //关闭model
		 $scope.onClose = function() {
			$modalInstance.dismiss();
		 }
		 
		 //确定事件
		 $scope.add = function() {
			 if(checkRows) {
				 $modalInstance.close(checkRows);
			 }
			 else {
				 Modal.alert("请选中一项进行保存操作!!!"); 
			 }
		 }
		 $scope.onSubmit();
		
    }]);
})();